<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>模糊加载 </title>
    <style>
        @import url('https://fonts.googleapis.com/css?family=Ubuntu');

        * {
        box-sizing: border-box;
        }

        body {
        font-family: 'Ubuntu', sans-serif;
        display: flex;
        align-items: center;
        justify-content: center;
        height: 100vh;
        overflow: hidden;
        margin: 0;
        }

        .bg {
        background: url('https://images.unsplash.com/photo-1576161787924-01bb08dad4a4?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2104&q=80')
            no-repeat center center/cover;
        position: absolute;
        top: -30px;
        left: -30px;
        width: calc(100vw + 60px);
        height: calc(100vh + 60px);
        z-index: -1;
        filter: blur(0px);
        }

        .loading-text {
        font-size: 50px;
        color: #fff;
        }
    </style>
</head>
<body>
    <section class="bg"></section>
    <div class="loading-text">0%</div>
    <script>
        // 选取样式为loading-text
        const text = document.querySelector('.loading-text');
        // 选择样式为bg的
        const bg = document.querySelector('.bg');
        // 加载进度为0%
        let load = 0;
        // 设置定时器，每隔30毫秒执行一次
        let int = setInterval(blurring, 30);
        // 实现模糊效果的函数
        function blurring() {
            // 加载进度加1
            load++
            // 当加载进度大于99时，清除定时器
            if(load > 99) {
                clearInterval(int);
            }
            // 显示加载进度
            text.innerText = `${load}%`;
            // 设置加载进度的透明度和模糊度
            text.style.opacity = scale(load, 0, 100, 1, 0);
            // 设置背景的模糊度
            bg.style.filter = `blur(${scale(load, 0, 100, 30, 0)}px)`;
        }
        // 线性映射函数
        const scale = (num, in_min, in_max, out_min, out_max) => {
            return (num - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
        }
    </script>
</body>
</html>